package com.china08.yxyapi.repository.mg;

import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;

import com.china08.yxyapi.entity.mg.office.YxyNoticeClass;

@Transactional
public interface YxyNoticeClassRepository extends MongoRepository<YxyNoticeClass, String> {

    YxyNoticeClass findById(String id);

    Page<YxyNoticeClass> findByClassIdAndSchoolIdOrderByCreatedDateDesc(String classId, String schoolId,
            Pageable pageable);

    Page<YxyNoticeClass> findByUserIdAndSchoolIdOrderByCreatedDateDesc(String userId, String schoolId,
            Pageable pageable);

    Page<YxyNoticeClass> findByUserIdAndSchoolIdAndClassIdOrderByCreatedDateDesc(String userId, String schoolId,
            String classId, Pageable pageable);

    Page<YxyNoticeClass> findByClassIdInAndSchoolIdOrderByCreatedDateDesc(List<String> yxyClassIdLst, String schoolId,
            Pageable pageable);

    @Query("{'receiver':{'$elemMatch': {'userId':?0,'isRead':?1}}}")
    List<YxyNoticeClass> findByReceiverUserIdAndReceiverIsRead(String userId, int isRead, Pageable pageable);

    @Query("{'school_id':?0, 'receiver':{'$elemMatch': {'userId':?1,'isRead':?2}}}")
    List<YxyNoticeClass> findBySchoolIdAndReceiverUserIdAndReceiverIsRead(String schoolId, String userId, int isRead,
            Pageable pageable);

    @Query("{'id':?0, 'school_id':?1, 'receiver':{'$elemMatch': {'userId':?2,'isRead':?3}}}")
    List<YxyNoticeClass> findByIdAndSchoolIdAndReceiverUserIdAndReceiverIsRead(String id, String schoolId,
            String userId, int isRead);

    @Query("{'receiver':{'$elemMatch': {'userId':{$in:?0},'isRead':?1}}}")
    List<YxyNoticeClass> findByReceiverUserIdInAndReceiverIsRead(List<String> childIds, int isRead, Pageable pageable);

    // 错误的方法
    // Integer countBySchoolIdAndReceiverUserIdAndReceiverIsRead(String
    // schoolId, String userId, int isRead);

    // 无误差的方法
    @Query(value = "{'school_id':?0,'receiver':{'$elemMatch': {'userId':?1,'isRead':?2}}}", count = true)
    Integer countBySchoolIdAndReceiverUserIdAndReceiverIsRead(String schoolId, String userId, int isRead);

    @Query(value = "{'receiver':{'$elemMatch': {'userId':{$in:?0},'isRead':?1}}}", count = true)
    Integer countByReceiverUserIdInAndReceiverIsRead(List<String> childIds, int isRead);

    @Query(value = "{'receiver':{'$elemMatch': {'userId':?0,'isRead':?1}}}", count = true)
    Integer countByReceiverUserIdAndReceiverIsRead(String childId, int isRead);

    @Query("{'id':?0,'receiver':{'$elemMatch': {'userId':{$in:?1},'isRead':?2}}}")
    List<YxyNoticeClass> findByIdAndReceiverUserIdInAndReceiverIsRead(String id, List<String> yxyStudentIdLst,
            int isRead);

    @Query(value = "{'created_date' : {$gte: ?0, $lte : ?1}}", count = true)
    List<YxyNoticeClass> findByCreatedDateBetweenAnd(Date start, Date end);

}
